﻿Imports dooll.Common

Partial Public Class template
    Inherits UI.IPage

    Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        EnableWebCache = True
        Dim XMLHTTP_MODE As String = Request.Headers.Get("HTTP_X_XMLHTTP_MODE")

        If Not String.IsNullOrEmpty(XMLHTTP_MODE) AndAlso XMLHTTP_MODE.ToLower = "true" Then
            IsXmlHttp = True
        End If

        Select Case uAction.ToLower
            'Case "xmlhttp"
            '    IsXmlHttp = True
            '    ReWriteHtmlFile()
            Case "xmlhttppost"
                IsXmlHttp = True
                ReWriteHtmlFile()
        End Select
    End Sub



    Protected Overloads Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Select Case uAction.ToLower
            Case "postlogin"
                Dim txtUser As String = Request.Form("txtUserName")
                Dim txtPass As String = Request.Form("txtPassword")
                Dim token As String = Request.Form("token")
                Dim toUrl As String = Request.Form("toUrl")
                If PwdErrorTimes >= 5 Then
                    Response.Clear()
                    Response.Write(Text.Alert("用户名和密码登录出错太多次。为了您帐号禁用5分钟!!", True))
                    Response.End()
                End If
                Response.Clear()
                If String.IsNullOrEmpty(txtUser) OrElse txtUser.Length < 3 OrElse String.IsNullOrEmpty(txtPass) OrElse txtPass.Length < 3 Then
                    Response.Write(Text.Alert("用户名和密码必须有3个字符!!", True))
                    Response.End()
                Else
                    Dim RtnValue As String() = Split(Login(txtUser, txtPass, token), ",")
                    If RtnValue(0) = "0" Then
                        If String.IsNullOrEmpty(toUrl) Then
                            If Not Request.UrlReferrer Is Nothing Then
                                toUrl = Request.UrlReferrer.AbsoluteUri
                            Else
                                toUrl = "/"
                            End If

                        End If
                        Response.Redirect(toUrl)
                        Response.End()
                    Else
                        Response.Write(Text.Alert(RtnValue(1), True))
                        Response.End()
                    End If
                End If

            Case "httpchklogin"
                If PwdErrorTimes >= 3 AndAlso Not ChkVaidCode() Then
                    Response.Clear()
                    Response.Write(Util.ReturnValue("103", "多次验证错误(需要验证码)!!"))
                    Response.End()
                End If
                Dim txtUser As String = Request.Form("txtUserName")
                Dim txtPass As String = Request.Form("txtPassword")
                Dim token As String = Request.Form("token")
                Response.Clear()
                If String.IsNullOrEmpty(txtUser) OrElse txtUser.Length < 3 OrElse String.IsNullOrEmpty(txtPass) OrElse txtPass.Length < 3 Then
                    Response.Write(Util.ReturnValue("101", "用户名和密码必须有3个字符!!"))
                Else
                    Response.Write(Login(txtUser, txtPass, token))
                End If

                Response.End()


            Case "xmlhttppost"

                Dim fTableLabelName As String = Request.Form("f")

                If String.IsNullOrEmpty(fTableLabelName) Then
                    Response.Clear()
                    Response.Write(Util.ReturnValue("102", "必需的参数不存在!!"))
                    Response.End()
                End If

                SavePostContent(fTableLabelName)

            Case "logout"
                LogOut(Request.QueryString("toUrl"))


            Case Else
                REM XMLHTTP MODE
                If IsXmlHttp Then
                    Call IsBasePath() '检查路径

                    Dim _F_ As String = Request.QueryString("f")

                    If String.IsNullOrEmpty(_F_) Then
                        Response.Write("404,缺少必要参数。")
                        Response.End()
                    End If

                    Response.Write(getHtmlData(_F_))
                    Response.End()
                Else
                    WriteOutHtml()
                End If

        End Select

    End Sub

    'Private Sub SavePostUpload(ByVal UploadFieldName As String)
    '    If Request.Files(UploadFieldName) Is Nothing Then
    '        Dim RemoteUrl As String = Request.Form(UploadFieldName)
    '        If String.IsNullOrEmpty(RemoteUrl) Then
    '            Response.Write(Util.OnUploadCompleted(1, "", "", "请填写远程文件."))
    '            Response.End()
    '        End If
    '        If Not RemoteUrl.StartsWith("http://", True, Nothing) Then
    '            Response.Write(Util.OnUploadCompleted(1, "", "", "非法远程文件，请以Http://开头."))
    '            Response.End()
    '        End If
    '        If String.IsNullOrEmpty(RemoteUrl) Then
    '            Response.Write(Util.OnUploadCompleted(1, "", "", "找不到上传的文件[" & RemoteUrl & "]!!!"))
    '            Response.End()
    '        ElseIf Not Regex.IsMatch(RemoteUrl, ".(" & Uploader.ImgTypes & ")$", RegexOptions.IgnoreCase) Then
    '            Response.Write(Util.OnUploadCompleted(1, "", "", "远程文件类型,系统不支持[" & RemoteUrl & "]!!!"))
    '            Response.End()
    '        Else
    '            UpRemoteFileToLocal(RemoteUrl, Passport.UserID)
    '            Response.End()
    '        End If
    '    End If
    'End Sub

    Protected Sub AliPayNotify()
        ''''''''''''''''''''''''以下参数是需要设置的相关配置参数，设置后不会更改的'''''''''''''''''''''''''

        Dim sArray As String() = GetRequestPost()

        If sArray.Length > 0 Then   '判断是否有带返回参数

            Dim notify_id As String = Request.Form("notify_id")
            Dim responseTxt = xAlipayVerify(notify_id)

            Dim sign As String = Request.Form("sign")           '获取支付宝反馈回来的sign结果
            Dim mysign As String = SignFromAlipay("post")           '获取通知返回后计算后（验证）的签名结果

            '写调试日志记录（若要调试，请把支付方式 debug=true）

            If PaymentConfigInfo.Alipay.debug Then
                Dim PreSignStr As String = Payment.Alipay.Util.create_linkstring(sArray)
                Dim sWord As New StringBuilder
                sWord.AppendLine("------------" & notify_id & "---------------")
                sWord.AppendLine("now=" & Now.ToString())
                sWord.AppendLine("responseTxt=" & responseTxt)
                sWord.AppendLine("sign=" & sign)
                sWord.AppendLine("mysign=" & mysign)
                sWord.AppendLine("notify回来的参数：" & PreSignStr)
                sWord.AppendLine("------------------  End  -----------------")
                Util.toLog(Server.MapPath("/log/" & DateTime.Now.ToString("yyyyMMdd") & ".txt"), sWord.ToString())
            End If


            If responseTxt = "true" And sign = mysign Then    '验证成功
                '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                '请在这里加上商户的业务逻辑程序代码

                '——请根据您的业务逻辑来编写程序（以下代码仅作参考）——
                '获取支付宝的通知返回参数，可参考技术文档中页面跳转同步通知参数列表
                Dim trade_no As String = Request.Form("trade_no")          '支付宝交易号
                Dim order_no As String = Request.Form("out_trade_no")      '获取订单号
                Dim total_fee As String = Request.Form("total_fee")        '获取总金额
                Dim subject As String = Request.Form("subject")            '商品名称、订单名称
                Dim body As String = Request.Form("body")                  '商品描述、订单备注、描述
                Dim buyer_email As String = Request.Form("buyer_email")    '买家支付宝账号
                Dim trade_status As String = Request.Form("trade_status")  '交易状态

                If Request.Form("trade_status") = "TRADE_FINISHED" Or Request.Form("trade_status") = "TRADE_SUCCESS" Then
                    '判断该笔订单是否在商户网站中已经做过处理（可参考“集成教程”中“3.4返回数据处理”）
                    '如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
                    '如果有做过处理，不执行商户的业务程序

                    Response.Write("success")       '请不要修改或删除
                Else
                    Response.Write("fail")          '请不要修改或删除
                End If
                '——请根据您的业务逻辑来编写程序（以上代码仅作参考）——

                '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            Else    '验证失败
                Response.Write("fail")              '请不要修改或删除
            End If
        Else
            Response.Write("无通知参数")            '请不要修改或删除
        End If
    End Sub
End Class